﻿using System;
using System.Collections.Generic;

namespace ShadeTree.Binding.Stylers
{
    public class Styler
    {
        public static readonly Styler Default = new Styler();

        private readonly List<IControlStyler> _stylers = new List<IControlStyler>();

        

        public void ClearAll()
        {
            _stylers.Clear();
        }

        public void UseStyleFor<T>(Action<T> action)
        {
            var styler = new ControlStyler<T>(action);
            _stylers.Add(styler);
        }

        public void ApplyStyle(object control)
        {
            foreach (IControlStyler styler in _stylers)
            {
                styler.Style(control);
            }
        }

        #region Nested type: ControlStyler

        internal class ControlStyler<T> : IControlStyler
        {
            private readonly Action<T> _styleAction;

            internal ControlStyler(Action<T> styleAction)
            {
                _styleAction = styleAction;
            }

            #region IControlStyler Members

            void IControlStyler.Style(object control)
            {
                if (typeof (T).IsAssignableFrom(control.GetType()))
                {
                    _styleAction((T) control);
                }
            }

            #endregion
        }

        #endregion

        #region Nested type: IControlStyler

        internal interface IControlStyler
        {
            void Style(object control);
        }

        #endregion
    }
}